package drds.binlog.event_filter.aviater;

import com.googlecode.aviator.AviatorEvaluator;
import drds.binlog.common.Authors;
import drds.binlog.data_object.Entry;
import drds.binlog.event_filter.EventFilter;
import drds.common.$;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * 基于aviater el表达式的匹配过滤
 */
@Author(name = Authors.LI_YANG)
public class ElFilter implements EventFilter<Entry>
{

    public static final String entry_key = "entry";
    @Setter
    @Getter
    private String expression;

    public ElFilter(String expression)
    {
        this.expression = expression;
    }

    public boolean match(Entry entry)
    {
        if ($.isNullOrEmpty(expression))
        {
            return true;
        }
        //
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(entry_key, entry);
        return (Boolean) AviatorEvaluator.execute(expression, map);
    }

}
